package com.weiadmin.controller.system;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiadmin.common.BaseController;
import com.weiadmin.common.PageParam;
import com.weiadmin.dao.MenusDao;
import com.weiadmin.model.system.AdminMenuModel;
import com.weiadmin.service.MenuService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述
 *
 * @author: shizhongyu
 * @date: 2023年06月02日 14:01
 */
@RestController
@RequestMapping("/admin/menus")
public class MenusController extends BaseController {

    @Autowired
    MenusDao dao;

    @Autowired
    MenuService service;
    @GetMapping("/index")
    public ModelAndView index(Model model) {
        ModelAndView modelAndView = display("menus/index");
        return modelAndView;
    }
    @GetMapping("/list")
    public ResponseEntity<?> list(PageParam pageParam)
    {
        Page<AdminMenuModel> result =  service.getList(pageParam);
        return page("请求成功",result);
    }

    @GetMapping("/alllist")
    public ResponseEntity<?> alllist()
    {
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        List<AdminMenuModel> menuList =  service.getAll();
//        for (AdminMenuModel item: menuList) {
//            TreeNode<String> node = new TreeNode<>(item.getId(), item.getParentId(), item.getTitle(), item.getId());
//            Map<String, Object> map = new HashMap<>();
//
//            node.setId(item.getId());
//            node.setName(item.getTitle());
//            node.setWeight(item.getId());
//            map.put("title",item.getTitle());
//            map.put("icon",item.getIcon());
//            map.put("href",item.getHref());
//            node.setExtra(map);
//
//            nodeList.add(node);
//        }
//        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");

        return success("请求成功",menuList);
    }

    @GetMapping("/add")
    public ModelAndView add(Model model) {
        ModelAndView modelAndView = display("menus/add");
        //查询一级菜单列表
        Map<String,Object> where = new HashMap<>();
        where.put("parent_id",0);
        List<AdminMenuModel> list = dao.selectByMap(where);
        model.addAttribute("parentList",list);
        return modelAndView;
    }

    @GetMapping("/edit")
    public ModelAndView edit(Model model,String id) {
        ModelAndView modelAndView = display("menus/edit");
        AdminMenuModel item =  service.getById(id);
        model.addAttribute("item",item);

        //查询一级菜单列表
        Map<String,Object> where = new HashMap<>();
        where.put("parent_id",0);
        List<AdminMenuModel> list = dao.selectByMap(where);
        model.addAttribute("parentList",list);

        return modelAndView;
    }

    @PostMapping("/addPost")
    public ResponseEntity<?> addPost(@RequestBody AdminMenuModel menu)
    {
        boolean result = service.save(menu);
        if(result){
            return success("添加成功！");
        }else{
            return error("添加失败,请稍后再试！");
        }
    }

    @PostMapping("/editPost")
    public ResponseEntity<?> editPost(@RequestBody AdminMenuModel menu)
    {
        boolean result = service.updateById(menu);
        if(result){
            return success("保存成功！");
        }else{
            return error("保存失败,请稍后再试！");
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> delete(@PathVariable String id)
    {
        if(StringUtils.isEmpty(id)){
            return error("ID不能为空！");
        }
        boolean result = service.removeById(id);
        if(result){
            return success("删除成功！");
        }else{
            return error("删除失败,请稍后再试！");
        }
    }
}
